package xin.lhuo.we.server.controller.user;

import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import xin.lhuo.we.server.entity.User;
import xin.lhuo.we.server.service.IHomeService;
import xin.lhuo.we.server.utils.IpUtil;
import xin.lhuo.we.server.utils.ResultVoUtil;
import xin.lhuo.we.server.vo.HomeVo;
import xin.lhuo.we.server.vo.ResultVo;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import static xin.lhuo.we.server.constant.HttpConstant.SESSION_USER;

/**
 * 统计数据
 *
 * @author Lhuo
 * @date 2019/5/22 0:02
 */
@RestController
@RequestMapping(value = "/home")
@Slf4j
public class HomeController {

    private final IHomeService iHomeServer;

    public HomeController(IHomeService iHomeServer) {
        this.iHomeServer = iHomeServer;
    }

    @GetMapping(value = "/count")
    public ResultVo getHomeData(HttpServletRequest request) {
        User user = getUser(request);
        HomeVo homeVo = iHomeServer.getHomeVo(user.getId());
        return ResultVoUtil.success(homeVo);
    }

    @GetMapping(value = "/weather")
    public ResultVo getWeather(HttpServletRequest request) {
        String ip = IpUtil.getIpAddr(request);
        return ResultVoUtil.success(iHomeServer.getWeather(ip));
    }

    @GetMapping(value = "/recently/photo")
    public ResultVo recentlyUpdatePhoto(HttpServletRequest request){
        return ResultVoUtil.success(iHomeServer.recentlyUpdatePhoto(getUser(request).getId()));
    }

    @GetMapping(value = "/recently/document")
    public ResultVo recentlyUpdateDocument(HttpServletRequest request){
        return ResultVoUtil.success(iHomeServer.recentlyUpdateDocument(getUser(request).getId()));
    }

    private User getUser(HttpServletRequest request) {
        HttpSession session = request.getSession();
        return (User) session.getAttribute(SESSION_USER);
    }
}
